package com.mapbar.ure.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ChangingMonitorHashSet<T> {
	private Set<T> set = new HashSet<T>();
	private boolean changing = false;

	public ChangingMonitorHashSet() {
	}

	public ChangingMonitorHashSet(Set<T> set) {
		this.set = set;
	}

	public boolean add(T i) {
		boolean added = set.add(i);
		if (added && !changing) {
			changing = true;
		}
		return added;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ChangingMonitorHashSet) {
			@SuppressWarnings("unchecked")
			ChangingMonitorHashSet<T> t = ((ChangingMonitorHashSet<T>) obj);
			return t.items().containsAll(set) && set.containsAll(t.items());
		} else {
			return false;
		}

	}

	public boolean hasAdded() {
		if (!changing) {
			return false;
		} else {
			boolean ret = changing;
			changing = false;
			return ret;
		}
	}

	public Set<T> items() {
		return set;
	}

	public boolean containsAll(ChangingMonitorHashSet<T> s) {
		return set.containsAll(s.set);
	}

	public boolean addAll(List<T> list) {
		boolean added = set.addAll(list);
		if (added && !changing) {
			changing = true;
		}
		return added;
	}
}
